package com.itheima.web.controller.cargo;


import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.cargo.*;
import com.itheima.service.cargo.ContractService;
import com.itheima.service.cargo.ExportService;
import com.itheima.service.cargo.PackingListService;
import com.itheima.service.cargo.ShippingOrderService;
import com.itheima.utils.DownloadUtil;
import com.itheima.web.controller.BaseController;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;

@Controller
@RequestMapping("/cargo/shipping")
public class ShippingOrderController extends BaseController {

    @Reference
    private ShippingOrderService shippingOrderService;

    @Reference
    private ExportService exportService;

    @Reference
    private PackingListService packingListService;

    @Reference
    private ContractService contractService;

    @RequestMapping(value = "/list", name = "委托列表管理查询")
    public String list(@RequestParam(value = "page", defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "10") Integer pageSize) {
        ShippingOrderExample shippingOrderExample = new ShippingOrderExample();
        PageInfo pageInfo = shippingOrderService.findByPage(pageNum, pageSize, shippingOrderExample);
        request.setAttribute("page", pageInfo);
        return "cargo/shipping/shipping-list";
    }

    @RequestMapping(value = "/exportE", name = "委托单校验")
    public String exportE(String id) {

        ShippingOrder shippingOrder = shippingOrderService.findById(id);
        shippingOrder.setState(1);
        shippingOrderService.update(shippingOrder);

        return "redirect:/cargo/shipping/list.do";
    }

    @RequestMapping(value = "/toAdd", name = "跳转委托添加页面")
    public String toAdd(String id) {
        //1.分析 页面需要三要素
        /*
            1.合同
            2.shippingOrderId
            3.export
         */
        PackingList packingList = packingListService.selectByPrimaryKey(id);
        String exportIds = packingList.getExportIds();
        String[] split = exportIds.split(",");
        Export export = exportService.findById(split[0]);

        String contractIds = export.getContractIds();
        String[] split1 = contractIds.split(",");
        Contract contract = contractService.findById(split1[0]);

        request.setAttribute("export", export);
        request.setAttribute("contract", contract);
        request.setAttribute("shippingOrderId", id);
        return "cargo/shipping/shipping-add";
    }

    @RequestMapping(value = "/add", name = "新增委托")
    public String add(ShippingOrder shippingOrder) {
        //补全信息
        shippingOrder.setCreateBy(getUser().getUserName());
        shippingOrder.setCreateDept(getUser().getDeptId());
        shippingOrder.setCreateTime(new Date());
        shippingOrderService.save(shippingOrder);
        return "redirect:/cargo/shipping/list.do";
    }

    @RequestMapping(value = "/toUpdate", name = "跳转修改页面")
    public String toUpdate(String id) {
        //1,根据id查询委托单信息
        ShippingOrder shippingOrder = shippingOrderService.findById(id);
        request.setAttribute("shippingOrder", shippingOrder);
        return "cargo/shipping/shipping-update";
    }

    @RequestMapping(value = "/edit", name = "委托单修改")
    public String edit(ShippingOrder shippingOrder) {
        shippingOrder.setCreateBy(getUser().getUserName());
        shippingOrder.setCreateDept(getUser().getDeptId());
        shippingOrderService.update(shippingOrder);

        return "redirect:/cargo/shipping/list.do";
    }


    @RequestMapping(value = "/delete", name = "委托单的删除")
    public String delete(String id) {
        shippingOrderService.delete(id);
        return "redirect:/cargo/shipping/list.do";
    }

    @RequestMapping(value = "/printExcelwithTemplate", name = "委托单导出--使用模板")
    public void printExcelwithTemplate(String id) throws IOException {

        ShippingOrder shippingOrder = shippingOrderService.findById(id);
        PackingList packingList = packingListService.selectByPrimaryKey(id);

        //2 封装execl
        //2-1) 读取模板文件---工作簿
        String realPath = session.getServletContext().getRealPath("/make/xlsprint/template01.xlsx");
        Workbook workbook = new XSSFWorkbook(new FileInputStream(new File(realPath)));

        Sheet sheet = workbook.getSheetAt(0);

        Row row3 = sheet.createRow(2);
        row3.createCell(0).setCellValue(shippingOrder.getShipper());
        row3.createCell(1).setCellValue(shippingOrder.getConsignee());
        row3.createCell(2).setCellValue(shippingOrder.getNotifyParty());
        row3.createCell(3).setCellValue(shippingOrder.getShippingOrderId());
        row3.createCell(4).setCellValue(shippingOrder.getLcNo());

        Row row5 = sheet.createRow(4);
        row5.createCell(0).setCellValue(shippingOrder.getPortOfLoading());
        row5.createCell(1).setCellValue(shippingOrder.getPortOfTrans());
        row5.createCell(2).setCellValue(shippingOrder.getPortOfDischarge());
        row5.createCell(3).setCellValue(shippingOrder.getLoadingDate());
        row5.createCell(4).setCellValue(shippingOrder.getLimitDate());


        Row row7 = sheet.createRow(6);
        row7.createCell(0).setCellValue(shippingOrder.getIsBatch());
        row7.createCell(1).setCellValue(shippingOrder.getIsTrans());
        row7.createCell(2).setCellValue(shippingOrder.getCopyNum());
        row7.createCell(3).setCellValue(packingList.getTotalCartons());
        row7.createCell(4).setCellValue(packingList.getTotalWeight());
        row7.createCell(5).setCellValue(packingList.getTotalVolume());

        Row row9 = sheet.createRow(8);
        row9.createCell(0).setCellValue(shippingOrder.getSpecialCondition());
        row9.createCell(2).setCellValue(shippingOrder.getCheckBy());


        //3 下载
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        workbook.write(byteArrayOutputStream);
        DownloadUtil.download(byteArrayOutputStream, response, "委托单" + id + ".xlsx");
    }


}
